package echat.future.cn.echat.my.view;

import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import echat.future.cn.echat.R;
import echat.future.cn.echat.databinding.ActivityCapitalBinding;
import echat.future.cn.echat.my.adapter.CapitalAdapter;
import echat.future.cn.echat.my.api.UserApi;
import echat.future.cn.echat.my.bean.ExpenBean;
import echat.future.cn.echat.my.utils.HttpUtilsRequst;
import echat.future.cn.echat.my.utils.UserHelper;
import echat.future.cn.echat.my.weight.Navagation.AdapterNavagation;
import echat.future.cn.echat.my.weight.Navagation.IBaseNavagation;

/**
 * 收入或者支出明细
 */
public class CapitalActivity extends BaseActivity<ActivityCapitalBinding>{

    public static String EXTRA_TYPE = "EXTRA_TYPE";

    private int currentType = TYPE_EXOEBDITURE;
    /**
     * 支出1
     */
    public static int TYPE_EXOEBDITURE = 1;
    /**
     * 收入2
     */
    public static int TYPE_INCOME = 2;

    AdapterNavagation adapterNavagation;



    CapitalAdapter adapter;

    List<ExpenBean> mDatas = new ArrayList<>();

    TextView emptyView;

    @Override
    public int layoutId() {
        return R.layout.activity_capital;
    }

    @Override
    public IBaseNavagation navagation() {
        adapterNavagation = new AdapterNavagation(this);
        return adapterNavagation;
    }

    @Override
    public void initView() {
        currentType = getIntent().getIntExtra(EXTRA_TYPE,TYPE_EXOEBDITURE);
        adapterNavagation.setTitle(currentType == TYPE_EXOEBDITURE?"支出明细":"收入明细",16, R.color.white)
                .setNavagationBackgroudColor(R.color.color_161823).setBack(R.drawable.btn_back);

        adapter = new CapitalAdapter(mDatas);
        adapter.setType(currentType);

        emptyView = new TextView(this);
        emptyView.setTextSize(15);
        emptyView.setText("没有查询到任何数据");
        emptyView.setTextColor(getResources().getColor(R.color.color_666666));
        emptyView.setLayoutParams(new LinearLayoutCompat.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
        emptyView.setGravity(Gravity.CENTER);
        adapter.setEmptyView(emptyView);


        bind.recyclerView.setAdapter(adapter);
        bind.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        loadCapital(page);
        adapter.setOnLoadMoreListener(() -> {
            page ++;
            loadCapital(page);
        },bind.recyclerView);
        bind.swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadCapital(page);
            }
        });
    }
    int page = 1;
    /**
     * 加载数据
     * @param page
     */
    private void loadCapital(int page){
        if(page == 1){
            mDatas.clear();
            adapter.notifyDataSetChanged();
        }
        if(currentType == TYPE_EXOEBDITURE) {
            new HttpUtilsRequst<>(this, HttpUtilsRequst.retrofitClient.create(UserApi.class).getMyExpendList(UserHelper.getToken(),page))
                    .setmOnRequstSuccessListener((HttpUtilsRequst.OnRequstSuccessListener<List<ExpenBean>>) o -> {
                        bind.swipeLayout.setRefreshing(false);
                        adapter.loadMoreComplete();
                        mDatas.addAll(o);
                        if(o.size()<10){
                            adapter.loadMoreEnd();
                        }
                        adapter.notifyDataSetChanged();
                    })
                    .start();
        }else{
            new HttpUtilsRequst<>(this, HttpUtilsRequst.retrofitClient.create(UserApi.class).getMyIncome(UserHelper.getToken(),page+""))
                    .setmOnRequstSuccessListener((HttpUtilsRequst.OnRequstSuccessListener<List<ExpenBean>>) o -> {
                        bind.swipeLayout.setRefreshing(false);
                        adapter.loadMoreComplete();
                        mDatas.addAll(o);
                        if(o.size()<10){
                            adapter.loadMoreEnd();
                        }
                        adapter.notifyDataSetChanged();
                    })
                    .start();
        }
    }
}
